import axios from 'axios'
import { Message } from 'element-ui'

const service = axios.create({
    baseURL : 'http://localhost:8080/'
})

// 请求拦截
service.interceptors.request.use(request => {
    // 如果缓存中存在token 则给node发过去
    if(sessionStorage.getItem('token')){
        request.headers['x-header-token'] = sessionStorage.getItem('token')
    }
    return request
})

// 响应拦截
service.interceptors.response.use(config =>{
    return config.data
},error =>{
    // 请求有误, 给出提示
    Message({
        message: error.message,
        type: 'error',
        duration: 2 * 1000 // 提示两秒后消失
    })
    return Promise.reject(error)
})

export default service